<?php
/**  class.editor.php
 * 
 * Extend Page factory in order to create forms for the blog
 * 
 * @author Ted, tass2001-at-gmail-dot-com
 * @package phpsimpleblog
 * @version $Id: class.editor.php 13 2013-03-31 02:20:53Z tass2001@gmail.com $
 * @website https://code.google.com/p/phpsimpleblog/
 */
class Editor extends PageFactory{
    
    private function  __createCaches(){
        $cacher = new StaticContent();
        $cacher->cacheArchives();
        $cacher->cacheSidebar();
        $cacher->cacheSitemap();
    }
    
    public function deletePost($id){
        if(AUTH){
            $this->db->query(sprintf("DELETE FROM %s WHERE ID='%s'",ENTRY_TABLE,$this->esc($id)));
        }
        return header('Location: /');
    }
    
    
    public function editPost($id){
        if(!AUTH)
            return header('Location: /login');
        if($_REQUEST['action'] == 'edit'){
            $this->db->query(sprintf("UPDATE %s SET Title='%s',Body='%s',last_modified=NOW() WHERE ID='%s'",
                            ENTRY_TABLE,
                            $this->esc($_REQUEST['title']),
                            htmlspecialchars($this->esc($_REQUEST['body'])),
                            $id));
            $this->__createCaches();
            return json_encode(array('success' => true,'id' =>  $id));
        }else{
            $post_query = $this->db->query(sprintf("SELECT ID as POST_ID,title as TITLE,Body as BODY FROM %s WHERE ID='%s'",ENTRY_TABLE,$this->esc($id)));
            $post_data = $post_query->fetch_assoc();
            $this->appendNode($this->importFromTemplate('post_edit','form',$post_data));
            return $this->printPage();
        }
        
    }
    
    public function hidePost($id){
        $id = $this->esc($id);
        if(AUTH){
            $query = $this->db->query(sprintf("SELECT `hidden` FROM %s WHERE ID='%s'",ENTRY_TABLE,$id));
            $post_data = $query->fetch_assoc();
            $hidden_setting = ($post_data['hidden']) ? '0' : '1';
            $this->db->query(sprintf("UPDATE %s SET hidden='%s' WHERE ID='%s'",ENTRY_TABLE,$hidden_setting,$id));
        }
        return header('Location: /');
    }
    
    public function newComment($request_data,$post_id){
        if($request_data['body'] && $request_data['name'] && $post_id){
            $this->db->query(sprintf("INSERT INTO %s (`entry_id`,`body`,`date`,`name`,`email`,`approved`) VALUES('%s','%s',NOW(),'%s','%s','0')",
                                     COMMENT_TABLE,$this->esc($post_id,true),$this->esc($request_data['body'],true),$this->esc($request_data['name'],true),$this->esc($request_data['email'],true)));
            return json_encode(array('success' => true));
        }
        return json_encode(array('success' => false));
    }
    
    public function newPost($request_data){
        if(!AUTH)
            return header('Location: /login');
        if(isset($request_data['title']) && isset($request_data['body'])){
            $poster = unserialize(AUTH);
            $this->db->query(sprintf("INSERT INTO %s (`title`,`body`,`date`,`user_id`,`hidden`) VALUES('%s','%s',NOW(),'%s','%s')",
                ENTRY_TABLE,
                $this->esc($request_data['title']),$this->esc($request_data['body']),
                $this->esc($poster['ID']),$this->esc((int)$request_data['hidden']))) or die($this->db->error);
            $this->__createCaches();
            return json_encode(array('success' => true,'post_id' =>  $this->db->insert_id));
        }
        $this->appendNode($this->importFromTemplate('post_new','form'));
        return $this->printPage();   
    }
    
}
?>